package ch.fhnw.qualiscan.application.pages.tabs;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.elements.AdminQuestionForm;
import ch.fhnw.qualiscan.application.modals.AdminNewQuestion;
import ch.fhnw.qualiscan.application.models.DomainModel;
import ch.fhnw.qualiscan.application.models.QuestionModel;
import ch.fhnw.qualiscan.application.pages.Admin;
import ch.fhnw.qualiscan.core.db.pojos.Domain;
import ch.fhnw.qualiscan.core.db.pojos.DomainText;
import ch.fhnw.qualiscan.core.db.pojos.Question;
import ch.fhnw.qualiscan.core.libraries.NotificationLib;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;

import eu.livotov.tpt.i18n.TM;

/**
 * Edit questions
 *
 */
@SuppressWarnings("serial")
public class AdminTab2 extends VerticalLayout {
	
	private Admin admin;
	private DomainModel domainModel;
	private QuestionModel questionModel;
	
	private Panel p1;
	private Panel p2;
	
	public NativeSelect domainSelect;
	public Domain currentDomain;
	
	public AdminTab2(Admin admin) {
		this.admin = admin;
		domainModel = new DomainModel();
		questionModel = new QuestionModel();
		
		buildLayout();
	}
	
	public void buildLayout() {
		
		p1 = new Panel();
		p2 = new Panel();
		
		p1.addComponent(displayForm());
		addComponent(p1);
	}
	
	public HorizontalLayout displayForm() {
		HorizontalLayout domainSwitcher = new HorizontalLayout();
		domainSwitcher.setSpacing(true);
		domainSwitcher.setMargin(true);
		
		// load domains
		List<Domain> domains = domainModel.getDomains();
		
		// build index
		List<Integer> answers = new ArrayList<Integer>();
		for(int i = 1; i <= domains.size(); i++) {
			answers.add(i);
		}
		
		// native select
		domainSelect = new NativeSelect(null, answers);
		domainSelect.setNullSelectionAllowed(false);
		domainSelect.setImmediate(true);
		
		for(Domain o : domains) {
			DomainText dt = domainModel.getDomainText(o, admin.sessionHelper.getUserSession().getLanguage());
			domainSelect.setItemCaption(o.getId(), dt.getText());
		}
		
		domainSwitcher.addComponent(domainSelect);
		
		// button
		Button switchButton = new Button(TM.get("admin.tab2.selectbutton"));
		switchButton.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				if(domainSelect.getValue() != null)
				displayQuestions(domainSelect.getValue());
			}
		});
		
		domainSwitcher.addComponent(switchButton);
		
		return domainSwitcher;
	}
	
	public void displayQuestions(Object value) {
		
		p2.removeAllComponents();
		p2.addComponent(addQuestion());
		
		currentDomain = domainModel.getDomainById(Integer.parseInt((String) value.toString()));
		
		if(currentDomain != null) {
			addComponent(p2);
			
			Iterator<Question> questionIterator = currentDomain.getQuestions().iterator();

			int counter = 0;
			int subCounter = 0;
			while (questionIterator.hasNext()) {
				
				subCounter = 0;
				counter++;
				Question currentQuestion = (Question) questionIterator.next();
				String currentQuestionText = questionModel.getQuestionText(admin.userSession.getLanguage(),currentQuestion).getText();
				
				if(currentQuestion.getSubquestions().size() > 0) {
					
					AdminQuestionForm wSubquestionContainer = new AdminQuestionForm(currentQuestion, counter, admin.userSession.getLanguage(), false, this);
					p2.addComponent(wSubquestionContainer);
					
					//Question has subquestions
					Iterator subQuestionIterator = currentQuestion.getSubquestions().iterator();
					while(subQuestionIterator.hasNext()) {
						subCounter++;
						Question currentSubQuestion = (Question) subQuestionIterator.next();
						String currentSubQuestionText = questionModel.getQuestionText(admin.userSession.getLanguage(),currentSubQuestion).getText();
						
						AdminQuestionForm questionContainer = new AdminQuestionForm(currentSubQuestion, subCounter, admin.userSession.getLanguage(), true, this);
						p2.addComponent(questionContainer);
					}
				}
				else {
					//Question has no subquestion
					AdminQuestionForm questionContainer = new AdminQuestionForm(currentQuestion, counter, admin.userSession.getLanguage(), false, this);
					p2.addComponent(questionContainer);
				}
			}
			
		} else {
			NotificationLib.showError(TM.get("error"), TM.get("error.standard"));
		}
	}
	
	public HorizontalLayout addQuestion() {
		HorizontalLayout addQuestionWrap = new HorizontalLayout();
		addQuestionWrap.setMargin(true, true, true, false);
		
		Button addQuestionButton = new Button(TM.get("action.new"));
		addQuestionButton.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				admin.openModal(new AdminNewQuestion((AdminTab2) event.getButton().getParent().getParent().getParent().getParent()));
			}
		});
		addQuestionButton.setIcon(Config.ICON_ADD);
		
		addQuestionWrap.addComponent(addQuestionButton);
		return addQuestionWrap;
	}
	
	public void refresh() {
		this.removeAllComponents();
		buildLayout();
	}

}
